package com.mrobuy.jd.api;

import com.google.gson.Gson;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.domain.supplier.ProductManagementService.response.find.MyProductInfoDto;
import com.jd.open.api.sdk.domain.supplier.ProductManagementService.response.get.SingleProductInfoDto;
import com.jd.open.api.sdk.domain.supplier.ProductManagementService.response.get.SkuInfoDto;
import com.jd.open.api.sdk.request.supplier.VcItemProductGetRequest;
import com.jd.open.api.sdk.request.supplier.VcItemProductsFindRequest;
import com.jd.open.api.sdk.response.supplier.VcItemProductGetResponse;
import com.jd.open.api.sdk.response.supplier.VcItemProductsFindResponse;
import com.mrobuy.jdvc.entity.VCItem;
import com.mrobuy.jdvc.entity.VCProduct;
import com.mrobuy.util.IdentifierGen;

import java.util.ArrayList;
import java.util.List;

/**
 *  根据商品编号获取商品信息
 *
 *  jingdong.vc.item.product.get
 *
 *  <url>https://open.jd.com/home/home#/doc/api?apiCateId=86&apiId=1672&apiName=jingdong.vc.item.product.get</url>
 *
 *
 *
 */
public class ItemProductGetClient extends JosClient<VCProduct> {

    private static final int PAGE_SIZE = 10;

    private VcItemProductGetRequest request = new VcItemProductGetRequest  ();

    public ItemProductGetClient(JdClient client) {
        super(client);
    }

    @Override
    public VCProduct call() throws Exception {

        VcItemProductGetResponse response = client.execute(request);

        if (response.getJosResultDto().getIsSuccess()) {
            SingleProductInfoDto productInfo = response.getJosResultDto().getSingleObj();

            VCProduct  product = new VCProduct();
            product.setId(IdentifierGen.uuid());

            product.setWareId(productInfo.getWareId());

            // *     @FieldMeta(name = "NAME", title = "名称", length = 128)
            product.setName(productInfo.getName());

            // *     @FieldMeta(name = "SUBMIT_TIME", title = "提交时间", length = 20)
            // product.setSubmitTime(productInfo.getSubmitTime());

            // *     @FieldMeta(name = "STATE", title = "状态")
            //product.setState("");

            // *     @FieldMeta(name = "CID1", title = "第一分类ID(三级分类)", length = 20)
            product.setCid1(String.valueOf(productInfo.getCid1()));

            // *     @FieldMeta(name = "CIDNAME1", title = "第一分类名称", length = 20)
            product.setCidName1(productInfo.getCidName1());

            // *     @FieldMeta(name = "BRANDID", title = "品牌ID", length = 20)
            product.setBrandid(String.valueOf(productInfo.getBrandId()));
            product.setBrandName(productInfo.getBrandName());

            // *     @FieldMeta(name = "ZHBRAND", title = "中文品牌", length = 50)
            product.setZhbrand(productInfo.getZhBrand());

            // *     @FieldMeta(name = "ENBRAND", title = "英文品牌", length = 50)
            product.setEnbrand(productInfo.getEnBrand());

            // *     @FieldMeta(name = "MODEL", title = "商品型号", length = 50)
            product.setModel(productInfo.getModel());

            // *     @FieldMeta(name = "TEL", title = "服务电话", length = 50)
            product.setTel(productInfo.getTel());

            // *     @FieldMeta(name = "WEBSITE", title = "官方网址", length = 256)
            product.setWebsite(productInfo.getWebSite());

            // *     @FieldMeta(name = "ORIGINALPLACE", title = "产地", length = 20)
            product.setOriginalplace(productInfo.getOriginalPlace());   // 中国大陆

            // *     @FieldMeta(name = "WARRANTY", title = "质保", length = 20)
            product.setWarranty(productInfo.getWarranty());    // 年质保	质保（最多可输入6个汉字）

            // *     @FieldMeta(name = "SHELFLIFE", title = "保质期", length = 8)
            product.setShelflife(String.valueOf(productInfo.getShelfLife()));    // 	365	保质期(单位：天)

            // *     @FieldMeta(name = "SALERCODE", title = "销售员简码", length = 20)
            product.setSalercode(productInfo.getSalerCode());
            product.setSalerName(productInfo.getSalerName());

            // *     @FieldMeta(name = "PURCHASERCODE", title = "采购员简码", length = 20)
            product.setPurchasercode(productInfo.getPurchaserCode());
            product.setPurchaserName(productInfo.getPurchaserName());

            // *     @FieldMeta(name = "PACKING", title = "包装规格", length = 20)
            product.setPacking(String.valueOf(productInfo.getPacking())); // 包装规格（[1×N]请填写N的值）

            // *     @FieldMeta(name = "PACKTYPE", title = "包装规格", length = 8)
            product.setPacktype(String.valueOf(productInfo.getPackType())); // 特殊属性（1:普通商品，2:易碎品，3:裸装液体，4:带包装液体，5:按原包装出库）

            // *     @FieldMeta(name = "SKUUNIT", title = "销售单位", length = 8)
            product.setSkuunit(productInfo.getSkuUnit()); // 箱	销售单位（必须为VC接口提供的销售单位）

            // *     @FieldMeta(name = "PKGINFO", title = "包装清单", length = 256)
            product.setPkginfo(productInfo.getPkgInfo()); // USB线 × 1	包装清单（格式：USB线 × 1、AV线 × 1、配带 × 1、说明书 × 1、保修卡 × 1 ，最多可录入20000个字符）

            // *     @FieldMeta(name = "INTROHTML", title = "PC 端商品介绍", length = 10000)
            product.setIntrohtml(productInfo.getIntroHtml());    // 	PC端代码录入的商品介绍

            // *     @FieldMeta(name = "INTROMOBILE", title = "Mobile 端商品介绍", length = 10000)
            product.setIntromobile(productInfo.getIntroMobile());    // 	Mobile端代码录入的商品介绍

            // *     @FieldMeta(name = "INTRO", title = "商品介绍", length = 10000)
            product.setIntro("");    // 	原始的商品介绍

            // *     @FieldMeta(name = "VIDEOID", title = "视频介绍编号", length = 20)
            product.setVideoid(String.valueOf(productInfo.getVideoId()));    // 	视频介绍编号

            // *     @FieldMeta(name = "DANGERVALUE", title = "危险品的值", length = 20)
            product.setDangervalue(String.valueOf(productInfo.getDangerGoods()));    // 	danger_value	Number[]	否	1	危险品的值

            // *     @FieldMeta(name = "SYSP", title = "试用品标识", length = 8)
            product.setSysp(String.valueOf(productInfo.getSysp()));    // 	试用品标识[0:否 1:是]

            // *     @FieldMeta(name = "STOREPROPERTY", title = "冷链标记", length = 8)
            product.setStoreproperty(String.valueOf(productInfo.getStoreProperty()));    // 	冷链标记（1：控温（10 -18℃）;2：冷藏（0-8 ℃）;3：冷冻（零下12- 零下18 ℃）;4：深冷（零下30 ℃ ）;6:产地直送;7：鲜品（冷藏存常温配）;0或null：未设置）

            // *     @FieldMeta(name = "PRODUCTOILNUMBER", title = "成品油容量", length = 20)
            product.setProductoilnumber(String.valueOf(productInfo.getProductOilNumber()));    // 	成品油容量(正数，如果填写小数，小数位数长度不能超过6位)

            // *     @FieldMeta(name = "PRODUCTOILUNIT", title = "成品油容量", length = 20)
            product.setProductoilunit(productInfo.getProductOilUnit());    // 	成品油单位[大写的'L'(升)或者大写的'T'(吨)]

            // *     @FieldMeta(name = "GIFTSGOODS", title = "成品油容量", length = 8)
            product.setGiftsgoods(String.valueOf(productInfo.getGiftsGoods()));    // 	赠品标识[0:否 1:是不可销售 2：是可销售]

            // *     @FieldMeta(name = "DESIGNCONCEPT", title = "设计理念", length = 2000)
            product.setDesignconcept(productInfo.getDesignConcept());    // 	设计理念	设计理念

            // *     @FieldMeta(name = "AFTERSALEDESC", title = "售后保障", length = 8)
            product.setAftersaledesc(productInfo.getAfterSaleDesc());    // 	售后保障图文详情字符串

            // *     @FieldMeta(name = "WREADME", title = "规格参数手工录入文本", length = 100)
            product.setWreadme(productInfo.getWreadme());    // 	鞋品材料：橡胶	规格参数手工录入文本


            product.setVendorCode(productInfo.getVendorCode());
            product.setVendorName(productInfo.getVendorName());

            List<VCItem> vcItems = new ArrayList<>();

            List<SkuInfoDto> skuInfoDtos = productInfo.getSkuList();
            if ( skuInfoDtos!=null && skuInfoDtos.size() > 0 ) {
                for (SkuInfoDto skuInfoDto : skuInfoDtos) {
                    VCItem item = new VCItem();
                    item.setId(IdentifierGen.uuid());

                    item.setWareId(productInfo.getWareId());

                    // *     @FieldMeta(name = "SKUID", title = "商品SKU", length = 32)
                    item.setSkuId(skuInfoDto.getSkuId());

                    // *     @FieldMeta(name = "SKUNAME", title = "商品名称", length = 100)
                    item.setSkuName(skuInfoDto.getSkuName());

                    // *     @FieldMeta(name = "UUID", title = "uuid", length = 32)
                    item.setUuid(skuInfoDto.getUuid());

                    // *     @FieldMeta(name = "DIM1VAL", title = "第一维度的销售属性值", length = 100)
                    item.setDim1Val(skuInfoDto.getDim1Val());

                    // *     @FieldMeta(name = "DIM1SORT", title = "第一维度销售属性顺序", length = 8)
                    item.setDim1Sort(String.valueOf(skuInfoDto.getDim1Sort()));

                    // *     @FieldMeta(name = "DIM2VAL", title = "第二维度的销售属性值", length = 100)
                    item.setDim2Val(skuInfoDto.getDim2Val());

                    // *     @FieldMeta(name = "DIM2SORT", title = "第二维度销售属性顺序", length = 8)
                    item.setDim2Sort(String.valueOf(skuInfoDto.getDim2Sort()));

                    // *     @FieldMeta(name = "OTHERSALEATTRIBUTE", title = "高维度(dim不小于3)销售属性列表", length = 1000)
                    // TODO  高维度(dim不小于3)销售属性列表
                    // item.setOtherSaleAttribute(skuInfoDto.getOtherSaleAttribute());

                    // *     @FieldMeta(name = "MARKETPRICE", title = "市场价", length = 20)
                    item.setMarketPrice(skuInfoDto.getMarketPrice().stripTrailingZeros().toPlainString()); // 	900	市场价（小数点后保留2位）

                    // *     @FieldMeta(name = "PURCHASEPRICE", title = "采购价", length = 20)
                    item.setPurchasePrice(skuInfoDto.getPurchasePrice().stripTrailingZeros().toPlainString()); // 700	采购价（小数点后保留2位）

                    // *     @FieldMeta(name = "MEMBERPRICE", title = "京东价(", length = 20)
                    item.setMemberPrice(skuInfoDto.getMemberPrice().stripTrailingZeros().toPlainString()); // 700	800	京东价(（小数点后保留2位）

                    // *     @FieldMeta(name = "WEIGHT", title = "商品重量", length = 20)
                    item.setWeight(skuInfoDto.getWeight().stripTrailingZeros().toPlainString()); // 	1.123	商品重量(单位：千克，含包装)小数点后保留3位

                    // *     @FieldMeta(name = "LENGTH", title = "长度", length = 20)
                    item.setLength(String.valueOf(skuInfoDto.getLength())); // 	1.123	长度(单位：毫米，含包装)

                    // *     @FieldMeta(name = "WIDTH", title = "宽度", length = 20)
                    item.setWidth(String.valueOf(skuInfoDto.getWidth())); // 	1.123	宽度(单位：毫米，含包装)

                    // *     @FieldMeta(name = "HEIGHT", title = "高度", length = 20)
                    item.setHeight(String.valueOf(skuInfoDto.getHeight())); // 	1.123	高度(单位：毫米，含包装)

                    // *     @FieldMeta(name = "UPC", title = "UPC编码", length = 20)
                    item.setUpc(skuInfoDto.getUpc()); // UPC编码（只包含字母和数字）

                    // *     @FieldMeta(name = "ITEMNUM", title = "货号", length = 20)
                    item.setItemNum(skuInfoDto.getItemNum()); // 132 货号（货号不能包括中文，只能为英文或数字）

                    vcItems.add(item);
                }
            } else {
                // 非盖亚商品 没有 item 信息

                VCItem item = new VCItem();
                item.setId(IdentifierGen.uuid());

                item.setWareId(productInfo.getWareId());

                // *     @FieldMeta(name = "SKUID", title = "商品SKU", length = 32)
                item.setSkuId(productInfo.getWareId());

                // *     @FieldMeta(name = "SKUNAME", title = "商品名称", length = 100)
                item.setSkuName(productInfo.getName());

                // *     @FieldMeta(name = "MARKETPRICE", title = "市场价", length = 20)
                item.setMarketPrice(productInfo.getMarketPrice().stripTrailingZeros().toPlainString()); // 	900	市场价（小数点后保留2位）

                // *     @FieldMeta(name = "PURCHASEPRICE", title = "采购价", length = 20)
                item.setPurchasePrice(productInfo.getPurchasePrice().stripTrailingZeros().toPlainString()); // 700	采购价（小数点后保留2位）

                // *     @FieldMeta(name = "MEMBERPRICE", title = "京东价(", length = 20)
                item.setMemberPrice(productInfo.getMemberPrice().stripTrailingZeros().toPlainString()); // 700	800	京东价(（小数点后保留2位）

                // *     @FieldMeta(name = "WEIGHT", title = "商品重量", length = 20)
                item.setWeight(productInfo.getWeight().stripTrailingZeros().toPlainString()); // 	1.123	商品重量(单位：千克，含包装)小数点后保留3位

                // *     @FieldMeta(name = "LENGTH", title = "长度", length = 20)
                item.setLength(String.valueOf(productInfo.getLength())); // 	1.123	长度(单位：毫米，含包装)

                // *     @FieldMeta(name = "WIDTH", title = "宽度", length = 20)
                item.setWidth(String.valueOf(productInfo.getWidth())); // 	1.123	宽度(单位：毫米，含包装)

                // *     @FieldMeta(name = "HEIGHT", title = "高度", length = 20)
                item.setHeight(String.valueOf(productInfo.getHeight())); // 	1.123	高度(单位：毫米，含包装)

                // *     @FieldMeta(name = "UPC", title = "UPC编码", length = 20)
                item.setUpc(productInfo.getUpc()); // UPC编码（只包含字母和数字）

                // *     @FieldMeta(name = "ITEMNUM", title = "货号", length = 20)
                item.setItemNum(productInfo.getItemNum()); // 132 货号（货号不能包括中文，只能为英文或数字）

                vcItems.add(item);
            }

            product.setVcItems(vcItems);

            return product;
        }

        return null;
    }

    public ItemProductGetClient setWareId(String ware_id) {
        request.setWareId(ware_id);
        return this;
    }
}
